package com.fy.controller;

import com.fy.dao.Ac_infoMapper;
import com.fy.dao.User_infoMapper;
import com.fy.entity.Ac_info;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/acinfo")
public class Ac_infoController {
    String id = "1";
    @Resource
    Ac_infoMapper ac_infoMapper;
    @Resource
    User_infoMapper userInfoMapper;

    @GetMapping("/getAll")
    public List<Ac_info> ac_infoMapper() {
        return ac_infoMapper.findAll();
    }

    @PostMapping("/getInfo")
    public List<Ac_info> getinfo(String ac_id) {
        List<Ac_info> co = ac_infoMapper.getInfo(ac_id);
        return co;
    }

    @PostMapping("/insert")
    public String insert(String ac_id, String ac_pic, String ac_title, String ac_author,
                         String info_id, String ac_info, int ac_like, String ac_add,
                         int ac_collection, String ac_state) {
        ac_infoMapper.insert_ac_info(id, ac_pic, ac_title, ac_author, info_id, ac_info, ac_like, ac_add, ac_collection, ac_state);
        id = (Integer.valueOf(id) + 1 + "");
        userInfoMapper.update_info(null, null, null, null, 2, info_id);
        return "200";
    }

    @PostMapping("/delete")
    public String delete(String ac_id) {
        ac_infoMapper.delete_ac_info(ac_id);
        return "200";
    }

    @PostMapping("/update")
    public String update(String ac_id, int ac_like, int ac_collection) {
        List<Ac_info> all = ac_infoMapper.findAll();
        for (Ac_info info : all) {
            if (ac_collection == 0) {
                if (info.getAc_id().equals(ac_id)) {
                    ac_like = info.getAc_like() + 1;
                    ac_collection = info.getAc_collection();
                }
            }
            if (ac_like == 0) {
                if (info.getAc_id().equals(ac_id)) {
                    ac_collection = info.getAc_collection() + 1;
                    ac_like = info.getAc_like();
                }
            }
            if (ac_collection == -1) {
                if (info.getAc_id().equals(ac_id)) {
                    ac_like = info.getAc_like() - 1;
                    ac_collection = info.getAc_collection();
                }
            }
            if (ac_like == -1) {
                if (info.getAc_id().equals(ac_id)) {
                    ac_collection = info.getAc_collection() - 1;
                    ac_like = info.getAc_like();
                }
            }
        }
        ac_infoMapper.update_ac_info(ac_like, ac_collection, ac_id);
        return "200";
    }

    @PostMapping("/limit")
    public int limit(int restrictions, String ac_id) {
        List<Ac_info> all = ac_infoMapper.findAll();
        for (Ac_info a : all) {
            if (a.getAc_id().equals(ac_id)) {
                int b = a.getRestrictions() - 1;
                ac_infoMapper.update_renshu(b, ac_id);
            }

        }
        return 200;
    }

}
